/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author DigitalPort
 */
public class PedidoDAO extends Model implements DaoGenerico {
 public boolean existe(Object o) throws SQLException {
        Integer id = (Integer) o;
        boolean sw = true;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            pstmt = getCon().prepareStatement("SELECT COUNT(*) FROM pedidos WHERE codigo_pedidos =?");
            pstmt.setInt(1, id.intValue());
            rs = pstmt.executeQuery();
            if (rs.next()) {
                if (rs.getString(1).equals("0")) {
                    sw = false;
                }
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }
        }
        System.out.print("Valor sw:" + sw);
        return sw;
    }

    public boolean existe1(Object o) throws SQLException {
        Integer id = (Integer) o;
        boolean sw = true;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sent = "select * from pedidos where codigo_pedidos like '%" + id.intValue() + "%'";
        try {
            pstmt = getCon().prepareStatement(sent);

            rs = pstmt.executeQuery();
            if (rs.next()) {
                if (rs.getString(1).equals("0")) {
                    sw = false;
                }
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }
        }
        System.out.print("Valor sw:" + sw);
        return sw;
    }

    public Object load(ResultSet rs) throws SQLException {
 Cliente_DAO cdao1 = new Cliente_DAO();
        Pedido p = new Pedido();
        p.setCodigo(rs.getInt(1));
        Integer idc= new Integer(rs.getInt(2));
        p.setCliente(cdao1.find(idc));
        p.setFechadelpedido(rs.getDate(3));
        p.setFechaentrga(rs.getDate(4));
        p.setSubtotal(rs.getDouble(5));
        p.setIva(rs.getDouble(6));
        p.setTotal(rs.getDouble(7));
       p.setEstado(rs.getString(8));

        return p;
    }

    public Object extrae(Object o) throws SQLException {
        Integer id = (Integer) o;

        Connection conex = getCon();

        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }
       Pedido p = new Pedido();
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            pstmt = conex.prepareStatement("SELECT * FROM pedidos WHERE codigo_pedidos=?");
            pstmt.setInt(1, id.intValue());
            rs = pstmt.executeQuery();
            if (rs.next()) {
                p = (Pedido) load(rs);

            } else {
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }
        }

        return p;
    }

    public void crear(Object o) throws SQLException {
        Pedido p = (Pedido) o;
        Connection conex = getCon();
        Integer id = new Integer(p.getCodigo());
        if (existe(id)) {
            throw new SQLException("Ya existe esta cliente");
        }
        PreparedStatement pstmt = null;

        try {

            pstmt = conex.prepareStatement("insert into pedidos values (?,?,?,?,?,?,?,?)");

            pstmt.setInt(1, p.getCodigo());
            pstmt.setInt(2, p.getCliente().getCodigo());
            pstmt.setDate(3, (Date) p.getFechadelpedido());
            pstmt.setDate(4, (Date) p.getFechaentrga());
            pstmt.setDouble(5, p.getSubtotal());
            pstmt.setDouble(6, p.getIva());
            pstmt.setDouble(7, p.getTotal());
            pstmt.setString(8, p.getEstado());


            pstmt.executeUpdate();

        } finally {
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
        }

    }

    public List extrae1(Object cod1) throws SQLException {
        Integer cod = (Integer) cod1;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Pedido p = null;
        List lista = new LinkedList();
        String sent = "select * from pedidos where codigo_pedidos like '%" + cod.intValue()+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();

                    while (rs.next()) {
                        p = (Pedido) load(rs);
                        lista.add(p);
                    }






        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }


      public List extrae2(Object nom) throws SQLException {
        Integer nom1= (Integer) nom;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Pedido p = null;
        List lista = new LinkedList();
        String sent = "select * from pedidos where nombre codCLie '%"+nom1+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();


                    while (rs.next()) {
                        p = (Pedido) load(rs);
                        lista.add(p);
                    }


        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

        public List extrae3(Object nom) throws SQLException {
         Date  nom1 = (Date) nom;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Pedido p = null;
        List lista = new LinkedList();
        String sent = "select * from pedidos where fecha_pedido like '%"+nom1+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();

                    while (rs.next()) {
                        p = (Pedido) load(rs);
                        lista.add(p);
                    }





        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }



    public List listar() throws SQLException {
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Pedido p = null;
        List lista = new LinkedList();
        try {
            ps = (PreparedStatement) conex.prepareStatement("select * from pedidos ");
            rs = ps.executeQuery();

            while (rs.next()) {
                p = (Pedido) load(rs);
                lista.add(p);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

    public boolean update(Pedido p) throws SQLException {
        Connection conex = getCon();
        Integer id = new Integer(p.getCodigo());
ResultSet rs = null;
        if (!existe(id)) {
            throw new SQLException("Pedido no registrado " + p.getCodigo());
        }
        PreparedStatement pstmt = null;
        boolean res = false;
        try {
            Cliente_DAO cdao = new Cliente_DAO();

            pstmt = conex.prepareStatement("Update pedidos set codigo_pedidos  = ?, codClie = ?, fecha_pedido=?, fecha_entrega=?, subtotal=?,iva = ?, total = ?,estado  = ?  where codigo_pedidos = ?");
            pstmt.setInt(1, p.getCodigo());
            Integer idc= new Integer(rs.getInt(2));
            p.setCliente(cdao.find(idc));
            pstmt.setDate(3, (Date) p.getFechadelpedido());
            pstmt.setDate(4, (Date) p.getFechaentrga());
            pstmt.setDouble(5, p.getSubtotal());
            pstmt.setDouble(6, p.getIva());
            pstmt.setDouble(7, p.getTotal());
            pstmt.setString(8, p.getEstado());
            pstmt.executeUpdate();




            int r = pstmt.executeUpdate();
            if (r != 0) {
                res = true;
            }
        } finally {
            if (pstmt != null) {
                pstmt.close();
            }
        }
        return res;
    }



    public List listar(Object o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List listarlista(List l) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Cliente find(Integer id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
